package com.zhangmeng.online.exam.ui.controller;

import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
import com.zhangmeng.online.exam.ui.module.QuestionType;
import com.zhangmeng.online.exam.ui.module.SubjectType;
import com.zhangmeng.online.exam.ui.service.QuestionService;
import com.zhangmeng.online.exam.ui.service.SubjectService;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.FxUtils;
import javafx.beans.property.SimpleLongProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.util.Map;

import static com.zhangmeng.online.exam.ui.utils.FxUtils.FX_BEANS;

/**
 * @author zm
 * @date 2025/4/1 12:00
 * @version: 1.0
 */
public class QuestionEditController {

    @FXML
    public TextField scoreField;

    @FXML
    public TextField explanationField;

    @FXML
    public ComboBox<QuestionType> typeComboBox;

    public ComboBox<SubjectType> subjectComboBox;

    @FXML
    public TextField questionField;

    private Map<String, Object> question = null;

    private SimpleLongProperty id_property = new SimpleLongProperty(0);

    public void setQuestion(Map<String, Object> map){
        this.question = map;
        this.questionField.setText(map.get("question").toString());
        this.scoreField.setText(map.get("score").toString());
        this.explanationField.setText(map.get("explanation").toString());
        id_property.set(Long.parseLong(map.get("id").toString()));
    }

    @FXML
    public void initialize() {
        typeComboBox.getItems().addAll(QuestionService.getALLQuestionType());
        typeComboBox.getSelectionModel().selectFirst();
        typeComboBox.setConverter(new StringConverter<QuestionType>() {
            @Override
            public String toString(QuestionType object) {
                return object.getDesc();
            }

            @Override
            public QuestionType fromString(String string) {
                return null;
            }
        });


        subjectComboBox.getItems().addAll(SubjectService.getALLSubjectType());
        subjectComboBox.getSelectionModel().selectFirst();
        subjectComboBox.setConverter(new StringConverter<SubjectType>() {
            @Override
            public String toString(SubjectType object) {
                return object.getName();
            }

            @Override
            public SubjectType fromString(String string) {
                return null;
            }
        });
    }

    public void handleSave(ActionEvent actionEvent) {
        QuestionType questionType = typeComboBox.getSelectionModel().getSelectedItem();
        SubjectType subjectType = subjectComboBox.getSelectionModel().getSelectedItem();
        String question = questionField.getText();
        String score = scoreField.getText();
        String explanation = explanationField.getText();

        if (question.isEmpty()){
            AlertUtils.alert_warning("题目不能为空！");
            return;
        }

        if (score.isEmpty()){
            AlertUtils.alert_warning("分值不能为空！");
            return;
        }

        if (explanation.isEmpty()){
            AlertUtils.alert_warning("解析不能为空！");
            return;
        }

        if (questionType == null){
            AlertUtils.alert_warning("请选择题型！");
            return;
        }

        if (subjectType == null){
            AlertUtils.alert_warning("请选择科目！");
            return;
        }

        QuestionService.saveQuestion(questionType.getType(), subjectType.getId(), question, score, explanation,id_property.get());
        handleCancel();
    }

    public void handleCancel() {
        Stage window = (Stage) typeComboBox.getScene().getWindow();
        window.close();
        //刷新数据
        DynamicTableComponent dynamicTableComponent = FX_BEANS.get(FxUtils.Question_DynamicTableComponent);
        dynamicTableComponent.flushData();
    }
}
